﻿using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace DAO
{
    public class QuanLyGiaVeDAO
    {
        public static List<SuatChieu> layDSSuatChieu()
        {
            List<SuatChieu> dsSuatChieu = new List<SuatChieu>();
            string sql = "Select * from SuatChieu";
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataReader datar = cmd.ExecuteReader();
            while (datar.Read())
            {
                SuatChieu suatChieu = new SuatChieu();
                suatChieu.MaSuatChieu = datar["MaSuatChieu"].ToString();
                suatChieu.GioBatDau = (int)datar["GioBatDau"];
                suatChieu.GioKetThuc = (int)datar["GioKetThuc"];
                dsSuatChieu.Add(suatChieu);
            }
            cn.Close();
            return dsSuatChieu;
        }

        public static BindingList<BangGiaVe> getDSGiaVe()
        {
            BindingList<BangGiaVe> dsBangGia = new BindingList<BangGiaVe>();
            string sql = "Select * from BANGGIA";
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataReader datar = cmd.ExecuteReader();
            while (datar.Read())
            {
                BangGiaVe gv = new BangGiaVe();
                gv.MaSuatChieu = datar["MASUATCHIEU"].ToString();
                gv.MaDinhDang = (int)datar["MADINHDANGPHIM"];
                gv.GiaVe = float.Parse(datar["GIAVE"].ToString());
                gv.MaGiaVe = datar["MAGIAVE"].ToString();
                dsBangGia.Add(gv);
            }
            cn.Close();
            return dsBangGia;
        }

        public static string themGiaVe(BangGiaVe gv)
        {
            string kq="";
            try
            {
                string sql = "insert into BANGGIA values(@GiaTien, @MaDDPhim, @MaSC)";
                SqlConnection cn = DataProvider.ConnectionData();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("@GiaTien", gv.GiaVe);
                cmd.Parameters.AddWithValue("@MaDDPhim", gv.MaDinhDang);
                cmd.Parameters.AddWithValue("@MaSC", gv.MaSuatChieu);
                cmd.Parameters["@MaSC"].DbType = DbType.String;
                kq = cmd.ExecuteNonQuery().ToString();
            }
            catch (System.Exception ex)
            {
                kq = ex.ToString();
            }
            return kq;
        }

        public static string capNhatGiaVe(BangGiaVe gv)
        {
            string kq = "";
            try
            {
                string sql = "Update BANGGIA "+
                              "set GIAVE = @GiaTien" +
                              " where MADINHDANGPHIM = @MaDDPhim"+
                              " and MASUATCHIEU = @MaSC and MAGIAVE = @MaGiaVe";
                SqlConnection cn = DataProvider.ConnectionData();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("@GiaTien", gv.GiaVe);
                cmd.Parameters.AddWithValue("@MaDDPhim", gv.MaDinhDang);
                cmd.Parameters.AddWithValue("@MaSC", gv.MaSuatChieu);
                cmd.Parameters.AddWithValue("@MaGiaVe", gv.MaGiaVe);
                cmd.Parameters["@MaSC"].DbType = DbType.String;
                cmd.Parameters["@MaGiaVe"].DbType = DbType.String;
                kq = cmd.ExecuteNonQuery().ToString();
            }
            catch (SqlException ex)
            {
                kq = ex.Message;
               // System.Windows.Forms.MessageBox.Show("Hello");
            }
            return kq;
        }
    }
}
